from socket import *
from multiprocessing import Process
from dict_db import *

class Handle(Process):
    def __init__(self, coon,addr):
        self.coon=coon
        self.addr=addr
        self.db=DictData()
        super().__init__()
    def run(self):
        while True:
            request=self.coon.recv(128)
            if not request or request==b'E':
                break
#开启网络
class DictServer():
    def __init__(self, host="", port=0):
        self.host=host
        self.port=port
        self.adress=(host,port)
        self.sock=self.create_socket()

    def create_socket(self):
        sock=socket()
        sock.bind(self.adress)
        return sock
    def serve_forever(self):
        self.sock.listen(5)
        print("Listen the port %d" % self.port)
        # 　循环接收客户端连接
        while True:
            conn, addr = self.sock.accept()
            print("Connect from", addr)
            t = Handle(conn, addr)  # 创建线程
            t.start()


if __name__ =='__main__':
    dict =DictServer(host="0.0.0.0",port=8888)
    dict.serve_forever()

